package cn.iocoder.yudao.module.shop.controller.admin.giftcardrecord;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.shop.controller.admin.giftcardrecord.vo.*;
import cn.iocoder.yudao.module.shop.dal.dataobject.giftcardrecord.ShopGiftCardRecordDO;
import cn.iocoder.yudao.module.shop.convert.giftcardrecord.ShopGiftCardRecordConvert;
import cn.iocoder.yudao.module.shop.service.giftcardrecord.ShopGiftCardRecordService;

@Tag(name = "管理后台 - 礼品卡绑定记录")
@RestController
@RequestMapping("/shop/gift-card-record")
@Validated
public class ShopGiftCardRecordController {

    @Resource
    private ShopGiftCardRecordService giftCardRecordService;

    @PostMapping("/create")
    @Operation(summary = "创建礼品卡绑定记录")
    @PreAuthorize("@ss.hasPermission('shop:gift-card-record:create')")
    public CommonResult<Long> createGiftCardRecord(@Valid @RequestBody ShopGiftCardRecordCreateReqVO createReqVO) {
        return success(giftCardRecordService.createGiftCardRecord(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新礼品卡绑定记录")
    @PreAuthorize("@ss.hasPermission('shop:gift-card-record:update')")
    public CommonResult<Boolean> updateGiftCardRecord(@Valid @RequestBody ShopGiftCardRecordUpdateReqVO updateReqVO) {
        giftCardRecordService.updateGiftCardRecord(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除礼品卡绑定记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('shop:gift-card-record:delete')")
    public CommonResult<Boolean> deleteGiftCardRecord(@RequestParam("id") Long id) {
        giftCardRecordService.deleteGiftCardRecord(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得礼品卡绑定记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('shop:gift-card-record:query')")
    public CommonResult<ShopGiftCardRecordRespVO> getGiftCardRecord(@RequestParam("id") Long id) {
        ShopGiftCardRecordDO giftCardRecord = giftCardRecordService.getGiftCardRecord(id);
        return success(ShopGiftCardRecordConvert.INSTANCE.convert(giftCardRecord));
    }

    @GetMapping("/list")
    @Operation(summary = "获得礼品卡绑定记录列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('shop:gift-card-record:query')")
    public CommonResult<List<ShopGiftCardRecordRespVO>> getGiftCardRecordList(@RequestParam("ids") Collection<Long> ids) {
        List<ShopGiftCardRecordDO> list = giftCardRecordService.getGiftCardRecordList(ids);
        return success(ShopGiftCardRecordConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得礼品卡绑定记录分页")
    @PreAuthorize("@ss.hasPermission('shop:gift-card-record:query')")
    public CommonResult<PageResult<ShopGiftCardRecordRespVO>> getGiftCardRecordPage(@Valid ShopGiftCardRecordPageReqVO pageVO) {
        PageResult<ShopGiftCardRecordDO> pageResult = giftCardRecordService.getGiftCardRecordPage(pageVO);
        return success(ShopGiftCardRecordConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出礼品卡绑定记录 Excel")
    @PreAuthorize("@ss.hasPermission('shop:gift-card-record:export')")
    @OperateLog(type = EXPORT)
    public void exportGiftCardRecordExcel(@Valid ShopGiftCardRecordExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<ShopGiftCardRecordDO> list = giftCardRecordService.getGiftCardRecordList(exportReqVO);
        // 导出 Excel
        List<ShopGiftCardRecordExcelVO> datas = ShopGiftCardRecordConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "礼品卡绑定记录.xls", "数据", ShopGiftCardRecordExcelVO.class, datas);
    }

}
